<template>
  <div>
    <div class="top">
      <div class="header">
        <van-nav-bar
          title="会员码"
          left-arrow
          @click-left="onClickLeft"
          right-text="服务商码"
          @click-right="onClickRight"
          fixed
        />
      </div>

      <div class="top-content">
        <div class="content">
          <span class="money">{{vipCode.use_number}}</span>
          <span class="yuan">已使用（个）</span>
        </div>
        <div class="content">
          <span class="money">{{vipCode.all_number}}</span>
          <span class="yuan">总个数（个）</span>
        </div>
        <div class="content">
          <span class="money">{{vipCode.no_use_number}}</span>
          <span class="yuan">未使用（个）</span>
        </div>
      </div>
    </div>
    <div class="tab">
      <van-tabs>
        <van-tab title="已使用">
          <div class="div" v-for="(item,index) in codeList" :key="index">
            <img :src="item.avatar" class="img" />
            <div class="center">
              <span class="name">{{item.username}}</span>
              <span class="time">会员码：{{item.vip_code}}</span>
            </div>
            <div class="right">
              <div class="right-top">
                <img src="../assets/images/huiyuan@3x.png" v-if="huiyuanShow" />
                <img src="../assets/images/fuwushang.png" alt v-if="fuwushangShow" />
                <span>{{item.mobile_change}}</span>
              </div>
              <span class="add">{{item.use_time}}</span>
            </div>
          </div>
        </van-tab>
        <van-tab title="未使用">
          <div class="div" v-for="(item,index) in userCodeList" :key="index">
            <img src="../assets/images/huiyuan@3x.png" class="div-img" v-if="userCodeListMember" />
            <img src="../assets/images/fuwushang.png" alt class="div-img" v-if="userCodeListShow" />
            <div class="center">
              <span class="name" v-if="item.open==1">会员码：{{item.vip_code_change}}</span>
              <span class="name" v-if="item.open!=1">会员码：**** **** ****</span>
              <span class="time">有效期：{{item.code_time_end}}</span>
            </div>
            <div class="div-right">
              <van-button round @click="butClick(item.id)" v-if="item.open==0">{{userCodeState}}</van-button>
              <van-button round :class="{used:item.open!=0}" v-else>{{userCodeState}}</van-button>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script>
export default {
  name: "my_share_huiyuanma",
  data() {
    return {
      vipCode: {},
      codeList: [],
      huiyuanShow: false,
      fuwushangShow: false,
      userCodeList: [],
      userCodeListMember: false,
      userCodeListShow: false,
      userCodeState: "",
      uid: ""
    };
  },
  created() {},
  mounted() {
    if (this.$route.query.uid) {
      this.uid = this.$route.query.uid;
    }
    this.vipCodeNumber();
    this.useCodeList();
    this.noUserCodeList();
  },
  methods: {
    onClickLeft() {
      // 点击回退的时候当做地址回退
      this.$router.back();
    },
    onClickRight() {
      this.$router.push({
        path: "/fuwushangma",
        query: {
          uid: this.uid
        }
      });
    },
    async vipCodeNumber() {
      // 获取会员码个数
      let params = {
        token: this.$cookies.get("token")
      };
      const res = await this.$axios.post(this.$api.vipCodeNumber, params);
      if (res.code == 1) {
        this.vipCode = res.data;
      } else {
        this.$toast("获取会员码个数失败：" + res.msg);
      }
    },
    async useCodeList() {
      // 获取已使用的会员码列表
      let params = {
        token: this.$cookies.get("token"),
        page: ""
      };
      const res = await this.$axios.post(this.$api.useCodeList, params);
      if (res.code == 1) {
        this.codeList = res.data;
        this.codeList.forEach((item, index) => {
          if (item.code_type == 1) {
            this.huiyuanShow = true;
            this.fuwushangShow = false;
          } else {
            this.huiyuanShow = false;
            this.fuwushangShow = true;
          }
        });
      } else {
        this.$toast("获取已使用会员码列表失败：" + res.msg);
      }
    },
    async noUserCodeList() {
      //获取未使用会员码列表
      let params = {
        token: this.$cookies.get("token"),
        page: ""
      };
      const res = await this.$axios.post(this.$api.noUserCodeList, params);
      this.userCodeList = res.data;
      if (res.code == 1) {
        this.userCodeList.forEach((item, index) => {
          if (item.code_type == 1) {
            this.userCodeListShow = false;
            this.userCodeListMember = true;
          } else {
            this.userCodeListShow = true;
            this.userCodeListMember = false;
          }
          if (item.open == 0) {
            this.userCodeState = "使用";
          } else if (item.open == 1) {
            this.userCodeState = "使用";
          } else {
            this.userCodeState = "失效";
          }
        });
      } else {
        this.$toast("获取未使用会员码列表失败：" + res.msg);
      }
    },
    butClick(id) {
      this.openVipCode(id);
    },
    async openVipCode(id) {
      let params = {
        token: this.$cookies.get("token"),
        id: id
      };
      const res = await this.$axios.post(this.$api.openVipCode, params);
      if(res.code==1){
          this.noUserCodeList();
      };
    }
  }
};
</script>

<style scoped>
.header >>> .van-nav-bar .van-icon {
  color: #fff;
}
.header {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}
.van-nav-bar {
  background-color: #75c2f1;
  border: none;
}
.van-nav-bar__title {
  color: white !important;
}
.van-nav-bar__text {
  color: white !important;
}
.van-hairline--bottom::after {
  border-bottom-width: 0;
}
.top {
  background-image: url("../assets/images/bg@3x.png");
  height: 2.45rem;
  /* background-size: 100% 100%; */
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.top-content {
  height: 2.7rem;
  /* height: 77.55%; */
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding-left: 8%;
  padding-right: 7%;
  align-items: center;
}
.content {
  display: flex;
  flex-direction: column;
  text-align: center;
  color: white;
}
.money {
  font-size: 0.18rem;
  font-family: PingFangSC;
  font-weight: 400;
}
.yuan {
  font-size: 0.1rem;
  font-family: PingFangSC;
  font-weight: 400;
}
.tab >>> .van-tabs__line {
  background-color: white;
}
.tab >>> .van-tab--active {
  color: #00c6d8;
}
.tab >>> .van-tab {
  font-size: 0.16rem;
  font-family: PingFangSC;
  font-weight: 400;
}
.div {
  width: 90%;
  height: 0.6rem;
  background: white;
  margin-left: 5%;
  display: flex;
  margin-top: 0.09rem;
  margin-bottom: 0.09rem;
  border-radius: 0.05rem;
  box-shadow: 0px 0.02rem 0.08rem 0px rgba(70, 70, 70, 0.2);
}
.img {
  width: 0.42rem;
  height: 0.42rem;
  margin-left: 3%;
  display: flex;
  justify-content: center;
  margin-top: 2.5%;
  border-radius: 50%;
}
.div-img {
  width: 0.2rem;
  height: 0.2rem;
  margin-left: 5%;
  display: flex;
  justify-content: center;
  margin-top: 2.5%;
}
.center {
  width: 60%;
  padding-left: 3%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.name {
  font-size: 0.14rem;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  color: rgba(52, 52, 52, 1);
}
.time {
  font-size: 0.12rem;
  font-family: PingFangSC;
  font-weight: 400;
  color: rgba(139, 139, 139, 1);
  padding-top: 5%;
}
.right {
  width: 30%;
  display: flex;
  flex-direction: column;
  text-align: right;
  padding-right: 5%;
}
.right-top {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  margin-top: 0.1rem;
  margin-bottom: 6%;
}
.right-top img {
  width: 0.13rem;
  height: 0.13rem;
  margin-right: 5%;
}
.right-top span {
  font-size: 0.12rem;
  font-family: PingFangSC;
  font-weight: 400;
  color: rgba(139, 139, 139, 1);
  /* line-height:.24rem; */
}
.add {
  font-size: 0.12rem;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  color: rgba(0, 198, 216, 1);
  line-height: 0.2rem;
}
.divright {
  width: 30%;
  display: flex;
  flex-direction: column;
  text-align: right;
  padding-left: 5%;
}
.div-right >>> .van-button__text {
  color: white;
  text-align: center;
  margin-top: -0.08rem;
  padding-left: 0.13rem;
}
.div-right >>> .van-button {
  background: linear-gradient(
    0deg,
    rgba(1, 190, 208, 1) 0%,
    rgba(124, 193, 241, 1) 100%
  );
  width: 0.66rem;
  height: 0.28rem;
  margin-top: 20%;
  display: flex;
  padding-left: 5%;
  /* flex-direction: column; */
  /* justify-content: center; */
}
.div-right >>> .used {
  background: #bfbfbf;
}
</style>






